﻿﻿@model RecipeModel

@using Telerik.Web.Mvc.UI;

@Html.ValidationSummary(false)

@Html.HiddenFor(model => model.Id)
@Html.Telerik().TabStrip().Name("recipe-edit").Items(x =>
{
    x.Add().Text("Recipe Info").Content(TabInfo().ToHtmlString()).Selected(true);
    x.Add().Text("Recipe Blends").Content(RecipBlendInfo().ToHtmlString());

    //generate an event
    EngineContext.Current.Resolve<IEventPublisher>().Publish(new AdminTabStripCreated(x, "recipe-edit"));
})
@helper TabInfo()
{ 
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Name):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.IsActive):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.IsActive)
                @Html.ValidationMessageFor(model => model.IsActive)
            </td>
        </tr>
    </table>
    
}
@helper RecipBlendInfo()
{ 
    if (Model.Id > 0)
    {

        /*hide "add spec" table if no attributes are defined*/
        if (Model.AvailableBlends.Count > 0)
        {
    <div>
        @(Html.Telerik().Grid(Model.RecipeBlends)
                    .Name("recipeBlend-grid")
                    .DataKeys(keys =>
                       {
                           keys.Add(x => x.Id);
                       })
                    .Columns(columns =>
                    {
                        columns.Bound(x => x.Blend.Name).Width(200).ReadOnly();
                        columns.Bound(x => x.FinishTemperature).Width(100);
                        columns.Bound(x => x.Notes).Width(300);
                        columns.Bound(x => x.PercentOfRecipe).Width(100);
                        columns.Command(commands =>
                        {
                            commands.Edit().Text("Edit");
                            commands.Delete().Text("Delete");
                        }).Width(250);

                    })
                    .Editable(x =>
                    {
                        x.Mode(GridEditMode.InLine);
                    })
                    .DataBinding(dataBinding =>
                    {
                        dataBinding.Ajax().Select("RecipeBlendByRecipeIdList", "Coffee", new { recipeId = Model.Id })
                            .Update("RecipeBlendUpdate", "Coffee")
                            .Delete("RecipeBlendDelete", "Coffee");
                    })
                    .EnableCustomBinding(true))
    </div>
        }
    <p>
        <strong>Add New Recipe Blend</strong>
    </p>
    <script type="text/javascript">
        //Handles the click event on the button
        $(document).ready(function () {
            $('#addRecipeBlend').click(function () {
                var blendId = $("#@Html.FieldIdFor(model => model.RecipeBlend.Blend)").val();
                var finishTemp = $("#@Html.FieldIdFor(model => model.RecipeBlend.FinishTemperature)").val();
                var percent = $("#@Html.FieldIdFor(model => model.RecipeBlend.PercentOfRecipe)").val();
                var notes = $("#@Html.FieldIdFor(model => model.RecipeBlend.Notes)").val();
                
                $('#addRecipeBlend').attr('disabled', true);
                $.ajax({
                    cache:false,
                    type: "POST",
                    url: "@(Url.Action("RecipeBlendAdd", "Coffee"))",
                    data: { "recipeId": @Model.Id, "blendId": blendId, "finishTemp": finishTemp, "Notes": notes, 
                        "percent": percent },
                    success: function (data) {
                        var recipeBlendGrid = $("#recipeBlend-grid");
                        recipeBlendGrid.data('tGrid').ajaxRequest();
                        $('#addRecipeBlend').attr('disabled', false);
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert('Failed to add recipe blend.');
                        $('#addRecipeBlend').attr('disabled', false);
                    }  
                });
            });
        });
    </script>
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                Recipe Name:
            </td>
            <td class="adminData">
                @Html.DisplayTextFor(model => model.Name)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.RecipeBlend.Blend):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.RecipeBlend.Blend, Model.AvailableBlends)
                @Html.ValidationMessageFor(model => model.RecipeBlend.Blend.Name)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.RecipeBlend.FinishTemperature):
            </td>
            <td class="adminData">
                @Html.TextBoxFor(model => model.RecipeBlend.FinishTemperature)
                @Html.ValidationMessageFor(model => model.RecipeBlend.FinishTemperature)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.RecipeBlend.Notes):
            </td>
            <td class="adminData">
                @Html.TextBoxFor(model => model.RecipeBlend.Notes)
                @Html.ValidationMessageFor(model => model.RecipeBlend.Notes)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.RecipeBlend.PercentOfRecipe):
            </td>
            <td class="adminData">
                @Html.TextBoxFor(model => model.RecipeBlend.PercentOfRecipe)
                @Html.ValidationMessageFor(model => model.RecipeBlend.PercentOfRecipe)
            </td>
        </tr>
        <tr>
            <td colspan="2" align="left">
                <input type="button" id="addRecipeBlend" class="t-button" value="Add Recipe Blend" />
            </td>
        </tr>
    </table>
    
    }
}
